using UnityEngine;

public class GearBox : MonoBehaviour
{
	public car car1;

	public GameObject pointerSpeed;

	public GameObject pointerRPM;

	public float maxPitch = 2.4f;

	public float[] totalSteps;

	public float currentPitch = 1f;

	private float procentPitch;

	private float timeToShift;

	public float speed;

	public bool shiftinGear;

	private int currentGear = 1;

	private Quaternion pointRot;

	private void Awake()
	{
	}

	private void Update()
	{
		speed = GetComponent<Rigidbody>().velocity.magnitude * 3.6f;
	}

	private void FixedUpdate()
	{
		if (car1.controlled)
		{
			procentPitch = Procents(maxPitch - 1f, 1f);
			Transmission();
			pointerSpeed.transform.localRotation = Quaternion.Euler(Mathf.Abs(speed) * -0.9f, -90f, 0f);
			pointerRPM.transform.localRotation = Quaternion.Euler(-70f * (currentPitch - 1f), -90f, 0f);
		}
	}

	private float Procents(float value, float procents)
	{
		return value / 100f * procents;
	}

	private float ProcentOfValue(float firstValue, float secondValue)
	{
		return (int)(firstValue / (secondValue / 100f));
	}

	private void Transmission()
	{
		float secondValue = 0f;
		float firstValue = 0f;
		if (UnityEngine.Input.GetAxis("Vertical") > 0f)
		{
			if (!shiftinGear)
			{
				for (int i = 0; i < totalSteps.Length; i++)
				{
					secondValue = totalSteps[i + 1] - totalSteps[i];
					firstValue = speed - totalSteps[i];
					if (speed >= totalSteps[currentGear + 1] && currentGear == i)
					{
						shiftinGear = true;
					}
					if (speed < totalSteps[i + 1])
					{
						break;
					}
				}
				currentPitch = Mathf.Lerp(currentPitch, 1f + procentPitch * ProcentOfValue(firstValue, secondValue), 0.1f);
				if (currentPitch > maxPitch)
				{
					currentPitch = maxPitch;
				}
				timeToShift = 0f;
			}
		}
		else
		{
			currentPitch = Mathf.Lerp(currentPitch, 1f, 0.05f);
			for (int j = 0; j < totalSteps.Length; j++)
			{
				currentGear = j;
				if (speed < totalSteps[j + 1])
				{
					break;
				}
			}
		}
		if (shiftinGear)
		{
			currentPitch = Mathf.Lerp(currentPitch, 1f, 0.01f);
			timeToShift += Time.deltaTime;
			if ((double)timeToShift > 0.5)
			{
				shiftinGear = false;
				currentGear++;
			}
		}
		if (currentPitch < 1f)
		{
			currentPitch = 1f;
		}
	}
}
